package com.health.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.health.R;
import com.health.model.JBBean;

import java.util.ArrayList;
import java.util.List;

public class SearchDaAdapter2 extends RecyclerView.Adapter<SearchDaAdapter2.DataListViewHolder> {

    /*
    初始化布局
     */
    private final LayoutInflater mLayoutInflater;
    private static final String TAG = "SelectorAdapter";
    private Context mContext;
    String name;
    int count = 0;
    private OnClickListener onClickListener;
    private int clickNum = 0;
    private String diseaseTypes;
    private List<Boolean> isClick = new ArrayList<>();
    String sex = "";
    private List<JBBean> content;

    public SearchDaAdapter2(Context mContext, List<JBBean> content, int number, OnClickListener onClickListener) {
        this.mContext = mContext;
        mLayoutInflater = LayoutInflater.from(mContext);
        this.content = content;
        count = number;
        this.onClickListener = onClickListener;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public DataListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DataListViewHolder(mContext, mLayoutInflater.inflate(R.layout.jb_item2, null));
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindViewHolder(final DataListViewHolder holder, final int position) {

        final JBBean bean = content.get(position);
        holder.tvName.setText(bean.getJbName());
        holder.tvNum.setText(bean.getJbNum());

        holder.ll_home.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Drawable drawable = holder.tvName.getCompoundDrawables()[2];
                if (drawable == null) {
                    return false;
                }
                //drawleft 是 小于 ,drawright 是 大于
                //左右上下分别对应 0  1  2  3
                if (event.getX() > holder.tvName.getWidth() - holder.tvName.getCompoundDrawables()[2].getBounds().width()) {
                    //点击之后执行的事件
                    holder.tvName.setVisibility(View.GONE);
                    return false;
                }
                return false;
            }
        });
        //给isClick集合添加数据
        if (content.size() > 0) {
            for (int i = 0; i < content.size(); i++) {
                isClick.add(false);
            }
        }
        if (onClickListener != null) {
            holder.ll_home.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.e(TAG,"Are you Ok？");
                    int position = holder.getLayoutPosition();
                    //判断集合是否为空，集合是否有内容（集合为空表示没有 new 这个集合对象）
                    if (isClick != null && isClick.size() > 0) {
                        for (int i = 0; i < isClick.size(); i++) {
                            isClick.set(i, false);
                        }
                        isClick.set(position, true);
                        notifyDataSetChanged();
                    }
                    diseaseTypes = holder.tvName.getText().toString();
                    onClickListener.setOnClickItemListener(view, position, diseaseTypes, content.get(position).getJbId());
                }
            });
        }
//            //判断集合是否为空，集合是否有内容（集合为空表示没有 new 这个集合对象）
        if (isClick != null && isClick.size() > 0) {
            if (isClick.get(position)) {
                holder.tvName.setTextColor(mContext.getColor(R.color.white));
                holder.tvNum.setTextColor(mContext.getColor(R.color.white));
                holder.ll_home.setBackground(mContext.getDrawable(R.drawable.jb_bg2));

            } else {
                holder.tvName.setTextColor(Color.parseColor("#666D73"));
                holder.tvNum.setTextColor(Color.parseColor("#666D73"));
                holder.ll_home.setBackground(mContext.getDrawable(R.drawable.jb_bg));
            }
        }

    }

    class DataListViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName, tvNum;
        private LinearLayout ll_home;

        public DataListViewHolder(final Context mContext, View itemview) {
            super(itemview);

            tvName = (TextView) itemview.findViewById(R.id.tv_name);
            tvNum = (TextView) itemview.findViewById(R.id.tv_num);
            ll_home = (LinearLayout) itemview.findViewById(R.id.ll_home);

        }
    }

    @Override
    public int getItemCount() {
        return count;
    }

    public interface OnClickListener {
        void setOnClickItemListener(View view, int position, String name, String id);
    }
}
